package com.tuoyu.peopletest.view.ac;

import androidx.appcompat.app.AppCompatActivity;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.UtilsTransActivity;
import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.utils.SCLogUtils;
import com.tuoyu.peopletest.utils.SCUIUtils;

import java.util.List;

public class ScanActivity extends AppCompatActivity {

    private final static String TAG = "Scan02Activity";

    private ZXingView mZXingView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        mZXingView = findViewById(R.id.zxingview);
        boolean granted = PermissionUtils.isGranted(Manifest.permission.CAMERA);
        if (!granted) {
            PermissionUtils.permission(com.blankj.utilcode.constant.PermissionConstants.CAMERA).rationale(
                    new PermissionUtils.OnRationaleListener() {
                        @Override
                        public void rationale(UtilsTransActivity activity, PermissionUtils.OnRationaleListener.ShouldRequest shouldRequest) {
                            //拒绝, 代开设置页面

                        }
                    }).callback(new PermissionUtils.FullCallback() {
                @Override
                public void onGranted(List<String> permissionsGranted) {
                    //同意

                }

                @Override
                public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied) {
                    //权限拒接

                }
            }).request();
        } else {

        }

        mZXingView.setDelegate(new QRCodeView.Delegate() {
            @Override
            public void onScanQRCodeSuccess(String result) {
                SCLogUtils.e(TAG,"result="+result);
                SCUIUtils.getMainThreadHandler().postDelayed(()->{
                    Intent intent = new Intent();
                    intent.putExtra("data",result);
                    setResult(RESULT_OK, intent);
                    finish();
                },100);
            }

            @Override
            public void onCameraAmbientBrightnessChanged(boolean isDark) {

            }

            @Override
            public void onScanQRCodeOpenCameraError() {

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mZXingView.startCamera();
    }

    @Override
    protected void onResume() {
        super.onResume();

        mZXingView.startSpot();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mZXingView.stopSpot();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mZXingView.stopCamera();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mZXingView.onDestroy();
    }
}